package com.kantboot.wechat.applet.entity;


import com.alibaba.fastjson.JSON;
import com.kantboot.util.WebSentUtil;
import com.kantboot.wechat.applet.config.WechatAppletConfig;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.io.Serializable;

public class AuthCode2SessionEntity implements Serializable {

    public static String REQUEST_URL="https://api.weixin.qq.com/sns/jscode2session";

    @Data
    @Accessors(chain = true)
    @NoArgsConstructor
    public static class Param implements Serializable{
        private String appid;
        private String secret;
        private String js_code;
        private String grant_type="authorization_code";
    }

    @Data
    @Accessors(chain = true)
    @NoArgsConstructor
    public static class Result implements Serializable{
        private String openid;
        private String unionId;
        private String sessionKey;
        private String errcode;
        private String errmsg;

        private WechatAppletConfig wechatAppletConfig;

        public Result analysis(Param param){
            param.setAppid(wechatAppletConfig.getAppid()).setSecret(wechatAppletConfig.getSecret());
            String result
                    = WebSentUtil
                        .sendGet(AuthCode2SessionEntity.REQUEST_URL,param);
            System.err.println(result+"--------");
            return JSON.parseObject(result, Result.class);
        }
    }

}
